home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Inne / Gry / Carnage_Contest / scripts / CC Original / weapons / Homing Missile.lua < prev    next >
Text File  |  2010-09-22  |  7KB  |  186 lines

  1. --------------------------------------------------------------------------------
  2. -- Weapon Homing Missile Launcher + Projectile Missile
  3. -- Original Carnage Contest Weapon
  4. -- Script by DC, August 2009, www.UnrealSoftware.de
  5. --------------------------------------------------------------------------------
  6.  
  7. -- Setup Tables
  8. if cc==nil then cc={} end
  9. cc.homing={}
  10. cc.homing.missile={}
  11.  
  12. -- Load & Prepare Ressources
  13. cc.homing.gfx_wpn=loadgfx("weapons/launcher.bmp")                    -- Weapon Image
  14. setmidhandle(cc.homing.gfx_wpn)
  15. cc.homing.gfx_pro=loadgfx("weapons/homingmissile.bmp")                -- Projectile Image
  16. setmidhandle(cc.homing.gfx_pro)
  17. cc.homing.sfx_attack=loadsfx("rocketrelease.wav")                    -- Attack Sound
  18. cc.homing.sfx_homing=loadsfx("homing.wav")                            -- Homing Sound
  19.  
  20. --------------------------------------------------------------------------------
  21. -- Weapon: Homing Missile Launcher
  22. --------------------------------------------------------------------------------
  23.  
  24. cc.homing.id=addweapon("cc.homing","Homing Missile",cc.homing.gfx_pro,1,2)    -- Add Weapon (1 use, first in round 2)
  25.  
  26. function cc.homing.draw()                                                    -- Draw
  27.     setblend(blend_alpha)
  28.     setalpha(1)
  29.     setcolor(120,180,255)
  30.     drawinhand(cc.homing.gfx_wpn,6,0)
  31.     -- HUD chargebar
  32.     if weapon_charge>0 and weapon_shots==0 then
  33.         hudchargebar(weapon_charge,100)
  34.     end
  35.     -- HUD Crosshair + Positioning
  36.     if weapon_shots==0 then
  37.         hudcrosshair(6,3)
  38.         hudpositioning(pos_normal)
  39.     end
  40. end
  41.  
  42. function cc.homing.attack(attack)                                    -- Attack
  43.     if (weapon_shots<=0) and(weapon_position==1) then
  44.         -- Charge
  45.         if (attack==1) then
  46.             weapon_charge=weapon_charge+1                            -- Increase charge
  47.         end
  48.         -- Fire a projectile (on release/full charge)
  49.         if (attack==0 and weapon_charge>0) or (weapon_charge>=100) then
  50.             -- No more weapon switching!
  51.             useweapon(0)
  52.             playsound(cc.homing.sfx_attack)
  53.             weapon_shots=weapon_shots+1
  54.             id=createprojectile(cc.homing.missile.id)
  55.             projectiles[id]={}
  56.             -- Ignore collision with current player at beginning
  57.             projectiles[id].ignore=playercurrent()
  58.             -- Set initial position of projectile
  59.             projectiles[id].x=getplayerx(0)+(6*getplayerdirection(0))+math.sin(math.rad(getplayerrotation(0)))*10.0
  60.             projectiles[id].y=getplayery(0)+3-math.cos(math.rad(getplayerrotation(0)))*10.0
  61.             -- Set Timer (time until homing, 1 sec)
  62.             projectiles[id].timer=50
  63.             -- Initial movement
  64.             projectiles[id].sx=math.sin(math.rad(getplayerrotation(0)))*20
  65.             projectiles[id].sy=-math.cos(math.rad(getplayerrotation(0)))*20
  66.             projectiles[id].x=projectiles[id].x-projectiles[id].sx
  67.             projectiles[id].y=projectiles[id].y-projectiles[id].sy
  68.             cc.homing.missile.move(id,0)
  69.             -- Set speed of projectile
  70.             projectiles[id].sx=math.sin(math.rad(getplayerrotation(0)))*(weapon_charge/100.0)*10.0
  71.             projectiles[id].sy=-math.cos(math.rad(getplayerrotation(0)))*(weapon_charge/100.0)*10.0
  72.             -- Effects
  73.             recoil(2)
  74.             -- End Turn
  75.             endturn()
  76.         end
  77.     end
  78. end
  79.  
  80. --------------------------------------------------------------------------------
  81. -- Projectile: Missile
  82. --------------------------------------------------------------------------------
  83.  
  84. cc.homing.missile.id=addprojectile("cc.homing.missile")        -- Add Projectile
  85.  
  86. function cc.homing.missile.draw(id)                            -- Draw
  87.     -- Setup draw mode
  88.     setblend(blend_alpha)
  89.     setalpha(1)
  90.     setcolor(255,255,255)
  91.     setscale(1,1)
  92.     -- Calculate projectile rotation
  93.     setrotation(math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy)))
  94.     -- Draw projectile
  95.     drawimage(cc.homing.gfx_pro,projectiles[id].x,projectiles[id].y)
  96.     -- Draw Arrow if out of Screen
  97.     outofscreenarrow(projectiles[id].x,projectiles[id].y)
  98. end
  99.  
  100. function cc.homing.missile.update(id)                        -- Update
  101.     cc.homing.missile.move(id,1)
  102. end
  103.  
  104. function cc.homing.missile.move(id,fx)
  105.     rot=math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy))
  106.     -- Particle Tail
  107.     if (fx==1) then
  108.         particle(p_smoke,projectiles[id].x-math.sin(math.rad(rot))*7,projectiles[id].y+math.cos(math.rad(rot))*7)
  109.         particlespeed(math.random(-2,2)*0.1,math.random(-2,2)*0.1)
  110.         particlefadealpha(0.01)
  111.         if projectiles[id].timer<0 and projectiles[id].timer>-250 then
  112.             particle(p_lightpuff,projectiles[id].x-math.sin(math.rad(rot))*6,projectiles[id].y+math.cos(math.rad(rot))*6)
  113.             particlefadealpha(0.04)
  114.         end
  115.     end
  116.     -- Wind + Gravity influence on speed
  117.     projectiles[id].sx=projectiles[id].sx+getwind()
  118.     projectiles[id].sy=projectiles[id].sy+getgravity()
  119.     -- Homing
  120.     explode=0
  121.     if (fx==1) then
  122.         projectiles[id].timer=projectiles[id].timer-1
  123.     end
  124.     if projectiles[id].timer<=0 and projectiles[id].timer>-250 then
  125.         -- Start Homing FX
  126.         if projectiles[id].timer==0 then
  127.             playsound(cc.homing.sfx_homing)
  128.             particle(p_ring,projectiles[id].x,projectiles[id].y)
  129.             particlecolor(0,150,255)
  130.         end
  131.         -- Homing Calculation
  132.         aimangle=math.deg(math.atan2(weapon_x-projectiles[id].x,-(weapon_y-projectiles[id].y)))
  133.         speed=math.sqrt(math.pow(projectiles[id].sx,2)+math.pow(projectiles[id].sy,2))
  134.         if speed<8.0 then
  135.             speed=speed+0.15
  136.             if speed>8.0 then speed=8.0 end
  137.         end
  138.         rot=rot+angledelta(rot,aimangle)*0.08
  139.         projectiles[id].sx=math.sin(math.rad(rot))*speed
  140.         projectiles[id].sy=-math.cos(math.rad(rot))*speed    
  141.         -- Timeout
  142.         if projectiles[id].timer<=-250 then
  143.             explode=1
  144.         end
  145.     end
  146.     -- Move (in substep loop for optimal collision precision)
  147.     msubt=math.ceil(math.max(math.abs(projectiles[id].sx),math.abs(projectiles[id].sy))/3)
  148.     msubx=projectiles[id].sx/msubt
  149.     msuby=projectiles[id].sy/msubt
  150.     for i=1,msubt,1 do
  151.         projectiles[id].x=projectiles[id].x+msubx
  152.         projectiles[id].y=projectiles[id].y+msuby
  153.         -- Collision
  154.         if collision(col3x3,projectiles[id].x+math.sin(math.rad(rot))*3,projectiles[id].y-math.cos(math.rad(rot))*3)==1 or explode==1 then
  155.             if terraincollision()==1 or objectcollision()>0 or playercollision()~=projectiles[id].ignore or explode==1 then
  156.                 -- Cause damage
  157.                 arealdamage(projectiles[id].x,projectiles[id].y,100,40)
  158.                 -- Destroy terrain
  159.                 terrainexplosion(projectiles[id].x,projectiles[id].y,25,1)
  160.                 -- Crater
  161.                 grey=math.random(0,40)
  162.                 if math.random(0,1)==1 then
  163.                     terrainalphaimage(gfx_crater100,projectiles[id].x,projectiles[id].y,math.random(6,9)*0.1,grey,grey,grey)
  164.                 else
  165.                     terrainalphaimage(gfx_crater125,projectiles[id].x,projectiles[id].y,math.random(6,9)*0.1,grey,grey,grey)
  166.                 end
  167.                 -- Free projectile
  168.                 freeprojectile(id)
  169.                 break
  170.             end
  171.         else
  172.             projectiles[id].ignore=0
  173.         end
  174.         -- Water
  175.         if (projectiles[id].y)>getwatery()+5 then
  176.             -- Effects
  177.             particle(p_waterhit,projectiles[id].x,projectiles[id].y)
  178.             playsound(sfx_hitwater1)
  179.             -- Free projectile
  180.             freeprojectile(id)
  181.             break
  182.         end
  183.     end
  184.     -- Scroll to projectile
  185.     scroll(projectiles[id].x,projectiles[id].y)
  186. end